home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xpuzzles.3 / xpuzzles / xpuzzles-5.3.1 / xoct / OctU.c < prev    next >
C/C++ Source or Header  |  1996-02-05  |  7KB  |  270 lines

  1. /*
  2. # X-BASED OCTAHEDRON
  3. #
  4. #  OctU.c
  5. #
  6. ###
  7. #
  8. #  Copyright (c) 1994 - 96    David Albert Bagley, bagleyd@hertz.njit.edu
  9. #
  10. #                   All Rights Reserved
  11. #
  12. #  Permission to use, copy, modify, and distribute this software and
  13. #  its documentation for any purpose and without fee is hereby granted,
  14. #  provided that the above copyright notice appear in all copies and
  15. #  that both that copyright notice and this permission notice appear in
  16. #  supporting documentation, and that the name of the author not be
  17. #  used in advertising or publicity pertaining to distribution of the
  18. #  software without specific, written prior permission.
  19. #
  20. #  This program is distributed in the hope that it will be "playable",
  21. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. #
  24. */
  25.  
  26. /* Undo algorithm */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <X11/IntrinsicP.h>
  31. #include <X11/Intrinsic.h>
  32. #include <X11/StringDefs.h>
  33. #include <X11/CoreP.h>
  34. #include "OctP.h"
  35.  
  36. typedef struct _MoveRecord
  37. {
  38.   /* int face, direction, style, control; */
  39.   unsigned short int packed; /* This makes assumptions on the data. */
  40.   int position; /* Do not make assumptions on this one. */
  41. } MoveRecord;
  42.  
  43. typedef struct _MoveStack
  44. {
  45.   MoveRecord move;
  46.   struct _MoveStack *previous, *next;
  47. } MoveStack;
  48.  
  49. static MoveStack *currMove, *lastMove, *firstMove;
  50. static int count;
  51. OctLoc *startLoc[MAXFACES] = {
  52.   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  53. };
  54.  
  55. static void InitStack();
  56. static void PushStack();
  57. static void PopStack();
  58. static int EmptyStack();
  59. static void FlushStack();
  60.  
  61. static void InitStack()
  62. {
  63.   if (!(lastMove = (MoveStack *) malloc(sizeof(MoveStack))))
  64.     XtError("Not enough memory, exiting.");
  65.   if (!(firstMove = (MoveStack *) malloc (sizeof (MoveStack))))
  66.     XtError("Not enough memory, exiting.");
  67.   firstMove->previous = lastMove->next = NULL;
  68.   firstMove->next = lastMove;
  69.   lastMove->previous = firstMove;
  70.   count = 0;
  71. }
  72.  
  73. static void PushStack(move)
  74.   MoveRecord move;
  75. {
  76.   if (!(currMove = (MoveStack *) malloc(sizeof(MoveStack))))
  77.     XtError("Not enough memory, exiting.");
  78.   lastMove->previous->next = currMove;
  79.   currMove->previous = lastMove->previous;
  80.   currMove->next = lastMove;
  81.   lastMove->previous = currMove;
  82.   currMove->move = move;
  83.   count++;
  84. }
  85.  
  86. static void PopStack(move)
  87.   MoveRecord *move;
  88. {
  89.   *move = lastMove->previous->move;
  90.   currMove = lastMove->previous;
  91.   lastMove->previous->previous->next = lastMove;
  92.   lastMove->previous = lastMove->previous->previous;
  93.   (void) free((void *) currMove);
  94.   count--;
  95. }
  96.  
  97. static int EmptyStack()
  98. {
  99.   return (lastMove->previous == firstMove);
  100. }
  101.  
  102. static void FlushStack()
  103. {
  104.   while (lastMove->previous != firstMove) {
  105.     currMove = lastMove->previous;
  106.     lastMove->previous->previous->next = lastMove;
  107.     lastMove->previous = lastMove->previous->previous;
  108.     (void) free((void *) currMove);
  109.   }
  110.   count = 0;
  111. }
  112.  
  113. /**********************************/
  114.  
  115. void InitMoves()
  116. {
  117.   InitStack();
  118. }
  119.  
  120. static void WriteMove(move, face, position, direction, style, control)
  121.   MoveRecord *move;
  122.   int face, position, direction, style, control;
  123. {
  124.   /* move->face = face; move->direction = direction; move->style = style;
  125.   move->control = control; */
  126.   move->packed = ((control & 0xF) << 12) + ((style & 0xF) << 8) +
  127.     ((direction & 0xF) << 4) + (face & 0xF);
  128.   move->position = position;
  129. }
  130.  
  131. static void ReadMove(face, position, direction, style, control, move)
  132.   int *face, *position, *direction, *style, *control;
  133.   MoveRecord move;
  134. {
  135.   /* *face = move.face; *direction = move.direction; *style = move.style;
  136.   *control = move.control; */
  137.   *face = move.packed & 0xF;
  138.   *direction = (move.packed >> 4) & 0xF;
  139.   *style = (move.packed >> 8) & 0xF;
  140.   *control = (move.packed >> 12) & 0xF;
  141.   *position = move.position;
  142. }
  143.  
  144. void PutMove(face, position, direction, style, control)
  145.   int face, position, direction, style, control;
  146. {
  147.   MoveRecord move;
  148.  
  149.   WriteMove(&move, face, position, direction, style, control);
  150.   PushStack(move);
  151. }
  152.  
  153. void GetMove(face, position, direction, style, control)
  154.   int *face, *position, *direction, *style, *control;
  155. {
  156.   MoveRecord move;
  157.  
  158.   PopStack(&move);
  159.   ReadMove(face, position, direction, style, control, move);
  160. }
  161.  
  162. int MadeMoves()
  163. {
  164.   return !EmptyStack();
  165. }
  166.  
  167. void FlushMoves(w)
  168.   OctWidget w;
  169. {
  170.   int face, position;
  171.  
  172.   FlushStack();
  173.   for (face = 0; face < MAXFACES; face++)
  174.     for (position = 0; position < w->oct.sizeSize; position++) {
  175.       startLoc[face][position].face =
  176.         w->oct.octaLoc[face][position].face;
  177.       startLoc[face][position].rotation =
  178.         w->oct.octaLoc[face][position].rotation;
  179.     }
  180. }
  181.  
  182. int NumMoves()
  183. {
  184.   return count;
  185. }
  186.  
  187. void ScanMoves(fp, w, moves)
  188.   FILE *fp;
  189.   OctWidget w;
  190.   int moves;
  191. {
  192.   int face, position, direction, style, control, l;
  193.   char c;
  194.  
  195.   for (l = 0; l < moves; l++) {
  196.     while ((c = getc(fp)) != EOF && c != SYMBOL);
  197.     (void) fscanf(fp, "%d %d %d %d %d",
  198.       &face, &position, &direction, &style, &control);
  199.     MoveOct(w, face, position, direction, style, control);
  200.   }
  201. }
  202.  
  203. void PrintMoves(fp)
  204.   FILE *fp;
  205. {
  206.   int face, position, direction, style, control, counter = 0;
  207.  
  208.   currMove = firstMove->next;
  209.   (void) fprintf(fp, "moves\tface\tpos\tdir\tstyle\tcon\n"); 
  210.   while (currMove != lastMove) {
  211.     ReadMove(&face, &position, &direction, &style, &control,
  212.       currMove->move);
  213.     (void) fprintf(fp, "%d%c\t%d\t%d\t%d\t%d\t%d\n",
  214.       ++counter, SYMBOL, face, position, direction, style, control); 
  215.     currMove = currMove->next;
  216.   }
  217. }
  218.  
  219. void ScanStartPosition(fp, w)       
  220.   FILE *fp;
  221.   OctWidget w;
  222. {
  223.   int face, position, num;
  224.   char c;
  225.  
  226.   while ((c = getc(fp)) != EOF && c != SYMBOL);
  227.   for (face = 0; face < MAXFACES; face++)
  228.     for (position = 0; position < w->oct.sizeSize; position++) {
  229.       (void) fscanf(fp, "%d ", &num);
  230.       startLoc[face][position].face = num;
  231.       if (w->oct.orient) {
  232.         (void) fscanf(fp, "%d ", &num);
  233.         startLoc[face][position].rotation = num;
  234.       }
  235.     }
  236. }
  237.  
  238. void PrintStartPosition(fp, w)       
  239.   FILE *fp;
  240.   OctWidget w;
  241. {
  242.   int face, position;
  243.  
  244.   (void) fprintf(fp, "\nstartingPosition%c\n", SYMBOL);
  245.   for (face = 0; face < MAXFACES; face++) {
  246.     for (position = 0; position < w->oct.sizeSize; position++) {
  247.       (void) fprintf(fp, "%d ", startLoc[face][position].face);
  248.       if (w->oct.orient)
  249.         (void) fprintf(fp, "%d  ", startLoc[face][position].rotation);
  250.     }
  251.     (void) fprintf(fp, "\n");
  252.   }
  253. }
  254.  
  255. void SetStartPosition(w)       
  256.   OctWidget w;
  257. {
  258.   int face, position;
  259.  
  260.   for (face = 0; face < MAXFACES; face++)
  261.     for (position = 0; position < w->oct.sizeSize; position++) {
  262.       w->oct.octaLoc[face][position].face =
  263.         startLoc[face][position].face;
  264.       if (w->oct.orient)
  265.         w->oct.octaLoc[face][position].rotation =
  266.           startLoc[face][position].rotation;
  267.     }
  268.   DrawAllPolyhedrons(w);
  269. }
  270.